package com.zhenyi.core.wechat.bridge;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;

public class WXThirdUtils {

	String component_appid;

	public WXThirdUtils(String component_appid) {
		this.component_appid = component_appid;
	}

	protected final boolean DEBUG = PropKit.getBoolean("devMode", false);

	public JSONObject thirdLogin(String appid, String code) {
		Cache cache = Redis.use();
		String url = "https://api.weixin.qq.com/sns/component/jscode2session?"
				+ "appid={}&js_code={}&grant_type=authorization_code&component_appid={}&component_access_token={}";
		String component_access_token = cache.get("component_access_token");
		url = StrFormatter.format(url, appid, code, component_appid, component_access_token);
		String response = DEBUG ? WXTools.get(url) : HttpUtil.get(url);
		JSONObject jsonObject = new JSONObject(response);
		return jsonObject;
	}

}
